﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using FFIV.Lib.Controlers.Admin;
using FFIV.Lib.IView.Users;
using FFIV.Lib.Model;

public partial class Views_Users_Manage : UserControl, IManageUsers
{
	private ManageUsersController m_Controller;

	public IEnumerable<UserInfos> Users
	{
		set
		{
			gv_Users.DataSource = value;
			gv_Users.DataBind();
		}
	}

	public ManageUsersController Controller
	{
		set { m_Controller = value; }
	}

	public void RegisteEvents()
	{
		gv_Users.RowDataBound += gv_Users_RowDataBound;
		gv_Users.RowEditing += gv_Users_RowEditing;
		gv_Users.RowCancelingEdit += gv_Users_RowCancelingEdit;
		gv_Users.RowUpdating += gv_Users_RowUpdating;
		gv_Users.RowDeleting += gv_Users_RowDeleting;
		gv_Users.PageIndexChanging += gv_Users_PageIndexChanging;
		gv_Users.RowCommand += gv_Users_RowCommand;
	}

	private void gv_Users_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		gv_Users.PageIndex = e.NewPageIndex;
		gv_Users.DataBind();
	}

	private void gv_Users_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		Guid userId = new Guid((gv_Users.DataKeys[e.RowIndex].Value).ToString());
		m_Controller.DeleteUser(userId);

		Response.Redirect(Request.CurrentExecutionFilePath);
	}

	private void gv_Users_RowEditing(object sender, GridViewEditEventArgs e)
	{
		gv_Users.EditIndex = e.NewEditIndex;
		m_Controller.LoadPage();
	}

	private void gv_Users_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		Response.Redirect(Request.CurrentExecutionFilePath);
	}

	private void gv_Users_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		Guid userId = new Guid((gv_Users.DataKeys[e.RowIndex].Value).ToString());

		string newName = ((TextBox)(gv_Users.Rows[e.RowIndex].Cells[0].Controls[1])).Text;

		m_Controller.UpdateUserInfo(userId, newName);
		Response.Redirect(Request.CurrentExecutionFilePath);
	}


	private void gv_Users_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		if (e.CommandName == "reset")
		{
			m_Controller.ResetPassword(new Guid(e.CommandArgument.ToString()));
		}
	}

	private void gv_Users_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType != DataControlRowType.DataRow)
			return;

		UserInfos rowView = (UserInfos)e.Row.DataItem;

		Label name = e.Row.FindControl("lbl_Name") as Label;
		if (name != null)
		{
			name.Text = rowView.UserName;
		}

		TextBox tbName = e.Row.FindControl("tb_Name") as TextBox;
		if (tbName != null)
		{
			tbName.Text = rowView.UserName;
		}

		Label email = e.Row.FindControl("lbl_Email") as Label;
		if (email != null)
		{
			email.Text = rowView.EMail;
		}

		Label createDate = e.Row.FindControl("lbl_CreateDate") as Label;
		if (createDate != null)
		{
			createDate.Text = rowView.CreateDate.ToString("yyyy-MM-dd");
		}

		Label lastDate = e.Row.FindControl("lbl_LastLoginDate") as Label;
		if (lastDate != null)
		{
			lastDate.Text = rowView.LastLoginDate.ToString("yyyy-MM-dd");
		}

		LinkButton reset = e.Row.FindControl("lbtn_ResetPasswrod") as LinkButton;
		if (reset != null)
		{
			reset.CommandArgument = rowView.UserId.ToString();
		}

		LinkButton editUser = e.Row.FindControl("lbtn_EditUser") as LinkButton;
		if (editUser != null)
		{
			editUser.CommandName = "edit";
		}

		LinkButton deleteUser = e.Row.FindControl("lbtn_DeletUser") as LinkButton;
		if (deleteUser != null)
		{
			deleteUser.CommandName = "delete";
		}

		LinkButton submit = e.Row.FindControl("lbtn_Submit") as LinkButton;
		if (submit != null)
		{
			submit.CommandName = "update";
		}

		LinkButton cancel = e.Row.FindControl("lbtn_Cancel") as LinkButton;
		if (cancel != null)
		{
			cancel.CommandName = "cancel";
		}
	}
}
